﻿Public Class frmClientMAJ
    Public pCd_cli As Integer

    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmClientMAJ_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Me.Text = "Fiche CLIENT : " & pCd_cli
        Me.BackColor = getSocieteColor()
        InitComboCabinets(CB_CABINETS)
        InitComboExperts(CB_EXPERTS, getTailCodeFromCombo(CB_CABINETS.SelectedItem))
        InitComboQualite(CB_QUALITE)
        InitComboTables(CB_MODEREG, "P_MODEREG")
        InitComboTables(CB_GROUPE, "P_GROUPE")
        InitComboTables(CB_TYPE, "P_TYPE")
        InitComboSocietes(CB_SOCIETES)
        ClientToScreen()
        TC_CLIENT.SelectedIndex = 0

    End Sub

    Private Sub ClientToScreen()

        ' Nouvelle Création ?
        If pCd_cli = 0 Then
            ' RAB des champs du form
            ClearTextBoxes(Me)
            TE_NOTARIF.Value = 1
            TE_DELAIREG.Value = 8
            CB_TYPE.SelectedIndex = CB_TYPE.FindString("P")
            CB_GROUPE.SelectedIndex = CB_GROUPE.FindString("X")
            CB_MODEREG.SelectedIndex = CB_MODEREG.FindString("1")
            BO_IMPRIMER.Enabled = False
        Else
            'Lire le CABINET
            szSQL = "CALL spGET_CLIENTS('" & gSocieteCode & "'," & pCd_cli & ")"
            GetRecordset(ctDSN_DEVFAC, szSQL, rs)
            If rs.RecordCount < 1 Then
                rs.Close()
                Exit Sub
            End If

            TE_NOM.Text = rs.Fields("nom").Value
            TE_ADRESSE.Text = nv(rs.Fields("ADRESSE").Value)
            TE_ADRESSE2.Text = nv(rs.Fields("ADRESSE2").Value)
            TE_CP.Text = nv(rs.Fields("CP").Value)
            TE_VILLE.Text = nv(rs.Fields("ville").Value)
            TE_TEL1.Text = nv(rs.Fields("tel1").Value)
            TE_TEL2.Text = nv(rs.Fields("tel2").Value)
            TE_TELP.Text = nv(rs.Fields("TELP").Value)
            TE_FAX.Text = nv(rs.Fields("FAX").Value)
            CB_QUALITE.SelectedIndex = CB_QUALITE.FindString(rs.Fields("qualite").Value.ToString)
            CB_CABINETS.SelectedIndex = getTailCodePosition(CB_CABINETS, rs.Fields("cabinet").Value.ToString)
            ' remplir le combo experts selon le cabinet lu
            InitComboExperts(CB_EXPERTS, getTailCodeFromCombo(CB_CABINETS.SelectedItem))
            CB_EXPERTS.SelectedIndex = getTailCodePosition(CB_EXPERTS, rs.Fields("expert").Value.ToString)
            CB_GROUPE.SelectedIndex = CB_GROUPE.FindString(rs.Fields("groupe").Value.ToString)
            CB_TYPE.SelectedIndex = CB_TYPE.FindString(rs.Fields("type").Value.ToString)
            CB_MODEREG.SelectedIndex = CB_MODEREG.FindString(rs.Fields("modereg").Value.ToString)
            TE_NOCOMPTE.Text = nv(rs.Fields("nocompte").Value)
            TE_DERNFACT.Text = nv(rs.Fields("dern_facture").Value)
            TE_DERNDOSSIER.Text = nv(rs.Fields("dern_operation").Value)
            TE_NOMRESP.Text = nv(rs.Fields("nomresp").Value)
            TE_AGREMENT.Text = nv(rs.Fields("agrement").Value)
            TE_WEBSITE.Text = nv(rs.Fields("website").Value)
            TE_NOTARIF.Text = rs.Fields("notarif").Value
            TE_DELAIREG.Text = rs.Fields("delaireg").Value
            TE_RELANCE.Text = rs.Fields("relance").Value
            TE_COMMENT.Text = nv(rs.Fields("comment").Value)
            TE_ETAGE.Text = nv(rs.Fields("etage").Value)
            TE_CODE.Text = nv(rs.Fields("code").Value)
            TE_EMAIL.Text = nv(rs.Fields("email").Value)
            TE_EMAIL.Text = nv(rs.Fields("email").Value)
            TE_CREE_LE.Text = "Créé le " & rs.Fields("cree_le").Value
            TE_MODIF_LE.Text = "Modifié le " & rs.Fields("modif_le").Value

            rs.Close()

            Me.Text = "Fiche CLIENT : " & pCd_cli & " - " & TE_NOM.Text
            BO_IMPRIMER.Enabled = True
            BO_SUPPRIMER.Enabled = True

        End If

    End Sub

    Private Sub BO_CABINET_Click(sender As System.Object, e As System.EventArgs) Handles BO_CABINET.Click
        ' afficher les détails 
        frmCabinetMAJ.pCode_cab = getTailCodeFromCombo(CB_CABINETS.SelectedItem)
        frmCabinetMAJ.ShowDialog()
    End Sub

    Private Sub BO_ENREGISTER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTER.Click

        If IsUserAuthorised(ctGestion) = False Then
            Exit Sub
        End If

        If TE_NOM.TextLength = 0 Then
            MsgBox("Nom du client est obligatoire")
            Exit Sub
        End If
        szSQL = "CALL spUPD_CLIENTS('" & gSocieteCode & "'," & pCd_cli & ",'" & CB_QUALITE.SelectedItem & "','" & nq(TE_NOM.Text) & "','" & _
                nq(TE_ADRESSE.Text) & "','" & nq(TE_ADRESSE2.Text) & "','" & TE_CP.Text & "','" & nq(TE_VILLE.Text) & "','" & TE_TEL1.Text & "','" & _
                nq(TE_NOMRESP.Text) & "','" & TE_TEL2.Text & "','" & TE_TELP.Text & "','" & TE_FAX.Text & "','" & getCodeFromCombo(CB_TYPE.SelectedItem) & "','" & _
                TE_AGREMENT.Text & "','" & TE_WEBSITE.Text & "'," & TE_NOTARIF.Value & "," & getCodeFromCombo(CB_MODEREG.SelectedItem) & "," & _
                TE_DELAIREG.Value & ",'" & TE_RELANCE.Text & "'," & getTailCodeFromCombo(CB_CABINETS.SelectedItem) & "," & getTailCodeFromCombo(CB_EXPERTS.SelectedItem) & ",'" & _
                getCodeFromCombo(CB_GROUPE.SelectedItem) & "','" & nq(TE_COMMENT.Text) & "','" & TE_ETAGE.Text & "','" & TE_CODE.Text & "','" & TE_EMAIL.Text & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)
        Me.Close()

    End Sub

    Private Sub BO_DUPPLIQUER_Click(sender As System.Object, e As System.EventArgs) Handles BO_DUPPLIQUER.Click
        TE_NOM_A_DUPPLIQUER.Text = TE_NOM.Text
        GB_DUPPLIQUER.Visible = True
        CB_SOCIETES.Focus()
    End Sub

    Private Sub BO_SUPPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_SUPPRIMER.Click

        If IsUserAuthorised(ctCompta) = False Then
            Exit Sub
        End If

        ' le client a-t-il des dossiers ?
        szSQL = "CALL spGET_DOSSIERS_byClient('" & gSocieteCode & "'," & pCd_cli & ")"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount > 0 Then
            MsgBox("Un dossier a déjà été créé pour ce client. Suppression impossible.", MsgBoxStyle.Critical)
            Exit Sub
        End If

        deleteEnregistrement("CLIENTS", TE_NOM.Text, pCd_cli)
        ' Cacher les détails
        Me.Close()

    End Sub

    Private Sub BO_ANNULER2_Click(sender As System.Object, e As System.EventArgs) Handles BO_ANNULER2.Click
        GB_DUPPLIQUER.Visible = False
    End Sub

    Private Sub BO_OK2_Click(sender As System.Object, e As System.EventArgs) Handles BO_OK2.Click
        ExecQuery(ctDSN_DEVFAC, "Call spDUP_CLIENTS('" & gSocieteCode & "'," & pCd_cli & ",'" & CB_SOCIETES.SelectedItem & "')")
        GB_DUPPLIQUER.Visible = False

    End Sub

    Private Sub CB_CABINETS_Click(sender As Object, e As System.EventArgs) Handles CB_CABINETS.Click
        InitComboExperts(CB_EXPERTS, getTailCodeFromCombo(CB_CABINETS.SelectedItem))

    End Sub

 
    Private Sub BO_IMPRIMER_Click(sender As System.Object, e As System.EventArgs) Handles BO_IMPRIMER.Click
        gCD_CLI = pCd_cli
        frmEditions.pRubrique = "CLIENT"
        frmEditions.ShowDialog()
    End Sub

    Private Sub TC_CLIENT_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TC_CLIENT.SelectedIndexChanged

        Select Case TC_CLIENT.SelectedIndex
            Case 0  ' tab Fiche

            Case 1  ' tab Dossiers
                Call InitDossiers(0)

        End Select

    End Sub

    Private Sub InitDossiers(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_DOSSIERS.Items.Clear()
        LV_DOSSIERS.ListViewItemSorter = Nothing

        ' lire toutes les dossiers du clients
        szSQL = "CALL spGET_DOSSIERS_byClient('" & gSocieteCode & "'," & pCd_cli & ")"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Do While Not rs.EOF
            LV_DOSSIERS.Items.Add(rs.Fields("date").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("type").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("numero").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("libelle").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("c_adr").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("montant").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(GetLibST(rs.Fields("status").Value))
            If rs.Fields("type").Value = "FA" Then
                LV_DOSSIERS.Items(nb).ForeColor = Color.Green
            End If
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            m_SortingColumn.Text = m_SortingColumn.Text.Replace("> ", "")
            SortListView(LV_DOSSIERS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_DOSSIERS)

    End Sub

    Private Sub LV_DOSSIERS_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_DOSSIERS.ColumnClick
        SortListView(LV_DOSSIERS, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column

    End Sub

    Private Sub BO_COMPTE_CLIENT_Click(sender As System.Object, e As System.EventArgs) Handles BO_COMPTE_CLIENT.Click
        ' afficher les comptes 
        frmClientCPT.pCd_cli = pCd_cli
        frmClientCPT.pNom_cli = TE_NOM.Text
        frmClientCPT.ShowDialog()
    End Sub

    Private Sub LV_DOSSIERS_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_DOSSIERS.DoubleClick

        If LV_DOSSIERS.SelectedItems.Count > 0 Then
            ' affiche les détails du dossier sélectionné
            Show_Dossier(LV_DOSSIERS.SelectedItems(0).SubItems(1).Text, LV_DOSSIERS.SelectedItems(0).SubItems(2).Text)
            ' réaffiche la viewlist
            InitDossiers(LV_DOSSIERS.SelectedItems(0).Index)
        End If

    End Sub

    Private Sub LV_DOSSIERS_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_DOSSIERS.KeyDown

        Select Case e.KeyCode
            Case Keys.Enter         ' Enter = DoubleClick
                LV_DOSSIERS_DoubleClick(sender, e)

        End Select

    End Sub
End Class

